<template>
  <div class="ed-config">
    <el-dialog
      class="risk-config"
      append-to-body
      :close-on-click-modal="false"
      :visible.sync="show"
      title="销售明细"
      width="80%"
    >
      <el-table v-loading="loading" :data="fruitsSalesInfoList" show-summary  :summary-method="getSummaries">
        <el-table-column type="selection" width="80" align="center" />
        <el-table-column type="index" label="序号" align="center"></el-table-column>
        <el-table-column label="水果名称" align="center" prop="fruitsName" />
        <el-table-column prop="unit" label="单位"  align="center" width="80">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.unit" :value="scope.row.unit"/>
          </template>
        </el-table-column>
        <el-table-column label="销售日期" align="center" prop="salesTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.salesTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="销售总额" align="center" prop="salesTotal" />
        <el-table-column label="销售数量" align="center" prop="salesNum" />
        <el-table-column label="零售价" align="center" prop="retailPrice" />
        <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="更新人名称" align="center">
          <template slot-scope="scope">
            {{
              selectUserDictLabel(
                'userId',
                'nickName',
                scope.row.updateBy
              )
            }}
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-dialog>
  </div>
</template>

<script>

import { listFruitsSalesInfo } from '@/api/fruits/fruitsSalesInfo'

export default {
  dicts: ['unit'],
  props: {

  },
  data() {
    return {
      show: false,
      fruitsSalesInfoList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fruitsId: null,
        salesTime: null,
        retailPrice: null,
        salesTotal: null,
        salesNum: null,
      },
      // 遮罩层
      loading: true,
      total: 0,
    }
  },

  methods: {
    setMetrics (salesTime) {
        this.queryParams.salesTime = salesTime;
        this.getList()
    },
    getList() {
      this.loading = true;
      listFruitsSalesInfo(this.queryParams).then(response => {
        this.fruitsSalesInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = {};
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (column.label === "销售总额" || column.label === "销售数量") {
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
            // sums[index] += ' kg';
          } else {
            // sums[index] = 'N/A';
          }
        }
      });
      return sums;
    },
  }
}
</script>
